package com.oracle.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.compents.PageResult;
import com.oracle.mapper.PhOrderMapper;
import com.oracle.pojo.PhOrder;
import com.oracle.pojo.PhOrderExample;
import com.oracle.pojo.PhOrderExample.Criteria;
import com.oracle.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	private PhOrderMapper orderMapper;

	/**
	 * 查询全部
	 */
	@Override
	public List<PhOrder> findAll() {
		return orderMapper.selecPhyExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		Page<PhOrder> page=   (Page<PhOrder>) orderMapper.selecPhyExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 */
	@Override
	public void add(PhOrder order) {
		orderMapper.insert(order);
	}


	/**
	 * 修改
	 */
	@Override
	public void update(PhOrder order){
		orderMapper.updateByPrimaryKey(order);
	}

	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public PhOrder findOne(Long id){
		return orderMapper.selecPhyPrimaryKey(id);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Long[] ids) {
		for(Long id:ids){
			orderMapper.deleteByPrimaryKey(id);
		}
	}


		@Override
	public PageResult findPage(PhOrder order, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);

		PhOrderExample example=new PhOrderExample();
		Criteria criteria = example.createCriteria();

		if(order!=null){
						if(order.getOrdernum()!=null && order.getOrdernum().length()>0){
				criteria.andOrdernumLike("%"+order.getOrdernum()+"%");
			}			if(order.getCreatetime()!=null && order.getCreatetime().length()>0){
				criteria.andCreatetimeLike("%"+order.getCreatetime()+"%");
			}
		}

		Page<PhOrder> page= (Page<PhOrder>)orderMapper.selecPhyExample(example);
		return new PageResult(page.getTotal(), page.getResult());
	}

}
